import config from '@/config/website';
export const exportFunc = (url, headers, parameter, filename) => {
  var xhh = new XMLHttpRequest()
  xhh.open("get", `${config.exportUrl}` + url + '?' + parameter)
  xhh.setRequestHeader('Blade-Auth', headers['Blade-Auth'])
  xhh.setRequestHeader('Content-Type', "application/json")
  xhh.responseType = 'blob'
  xhh.onload = function () {
    var blob = this.response
    var reader = new FileReader
    reader.readAsDataURL(blob)
    reader.onload = function (e) {
      var a = document.createElement('a')
      var body = document.querySelector('body')
      a.download = filename
      a.href = e.target.result
      body.append(a)
      a.click()
      body.removeChild(a)
    }
  }
  xhh.send()
}
function toQueryPair(key, value) {
  if (typeof value == 'undefined') {
    return key;
  }
  return key + '=' + encodeURIComponent(value === null ? '' : String(value));
}
export const toQueryString = (obj) => {
  var ret = [];
  for (var key in obj) {
    key = encodeURIComponent(key);
    var values = obj[key];
    if (values && values.constructor == Array) {//数组
      var queryValues = [];
      for (var i = 0, len = values.length, value; i < len; i++) {
        value = values[i];
        queryValues.push(toQueryPair(key, value));
      }
      ret = ret.concat(queryValues);
    } else { //字符串
      ret.push(toQueryPair(key, values));
    }
  }
  return ret.join('&');
}

export const checkValueNegative = (value, type) => {
  if (value == -1) {
    if (type == 1) {//Number
      return 0
    } else if (type == 2) {//String
      return ''
    }
  } else {
    return value
  }
}

export const maxCount = (list) => {
  var value = Math.max.apply(null, list);
  return value
}
export const avageCount = (list) => {
  var len = list.length
  var sum = 0
  list.map(item => {
    sum += parseInt(item)
  })
  var value = sum / len
  return value
}
export const sumCount = (list) => {
  var sum = 0
  list.map(item => {
    sum += parseInt(item)
  })
  return sum
} 

export const getTimeAfter21 = () => {
  var timer = new Date().getTime()
  var twentyone = 24*60*60*21*1000
  var time = timer+twentyone
  var date21 = new Date(time)
  var week = date21.getDay()
  var day0 = 24*60*60*5*1000
  var day1 = 24*60*60*4*1000
  var day2 = 24*60*60*3*1000
  var day3 = 24*60*60*2*1000
  var day4 = 24*60*60*1*1000
  var day6 = 24*60*60*6*1000
  var value = null
  if(week == 0){
    value =new Date(time+day0)
  }else if(week == 1){
    value =new Date(time+day1)
  }else if(week == 2){
    value = new Date(time+day2)
  }else if(week == 3){
    value = new Date(time+day3)
  }else if(week ==4){
    value = new Date(time+day4)
  }else if(week == 5){
    value = new Date(time)
  }else if(week == 6){
    value = new Date(time+day6)
  }
  var Y = value.getFullYear() + '-';
  var M = (value.getMonth() + 1 < 10 ? '0' + (value.getMonth() + 1) : value.getMonth() + 1) + '-';
  var D = value.getDate();
  return Y + M + D
}

export const culomnSpan = () => {
  let width = window.screen.width
  var culomnSpan=''
  if(width<=1024){
    culomnSpan = 24
  }else if(width>1024&&width<1440){
    culomnSpan = 12
  }else{
    culomnSpan = 8
  }
  return culomnSpan
}

export const searchSpan = (num) => {
  var n = num%3
  var h = num%2
  let width = window.screen.width
  var searchSpan=''
  if(width<=1024){
    searchSpan = 24
  }else if(width>1024&&width<1440){
    if(h == 0){
      searchSpan = 24
    }else if(h == 1){
      searchSpan = 12
    }
  }else{
    if(n == 0){
      searchSpan = 24
    }else if(n == 1){
      searchSpan = 16
    }else if(n == 2){
      searchSpan = 8
    }
  }
  return searchSpan
} 

export const formSpan = () => {
  let width = window.screen.width
  var formSpan=''
  if(width<=1024){
    formSpan = 8
  }else{
    formSpan = 6
  }
  return formSpan
}

export const accurateAdd =(formula,digit) =>{
    var m = Math.pow(10, digit);
    var list = formula.split('+')
    var IntList= []
    var flotList=[]
    var IntSub=0
    var flotSub=0
    list.map(item=>{
      if(item.indexOf('.') == -1){
        IntList.push(item)
      }else{
        var a = item.split('.')[0]
        var b = item.split('.')[1]
        IntList.push(a)
        flotList.push(b)
      }
    })
    IntList.map(item=>{
      IntSub+=Number(item)
    })
    flotList.map(item=>{
      var n = Math.pow(10, digit-item.length);
      flotSub+=Number(item)*n
    })
    var s = IntSub + flotSub/m
    return s.toFixed(5)
}

export const accurateReduce =(a,b,digit) =>{
  if(typeof(a) == 'number' && a!='NaN'){
    a = a.toString()
  }else if(a == 'NaN'){
    a = 0
  }
  if(typeof(b) == 'number' && b!='NaN'){
    b = b.toString()
  }else if(b == 'NaN'){
    b = 0
  }
  var m = Math.pow(10, digit);
  var x = 0 ;var y = 0;var xflot='';var yflot='';
  if(a.indexOf('.')== -1){
    x = a
  }else{
    x = a.split('.')[0]
    xflot = a.split('.')[1]
  }
  if(b.indexOf('.')== -1){
    y = b
  }else{
    y = b.split('.')[0]
    yflot = b.split('.')[1]
  }
  var n1 = Math.pow(10, digit-xflot.length);
  var n2 = Math.pow(10, digit-yflot.length);
  var s = (Number(x)-Number(y)) + (Number(xflot)*n1- Number(yflot)*n2)/m
  return s.toFixed(5)
}

export const accurateMultip =(a,b,digit) =>{
  if(typeof(a) == 'number' && a!='NaN'){
    a = a.toString()
  }else if(a == 'NaN'){
    a = 0
  }
  if(typeof(b) == 'number' && b!='NaN'){
    b = b.toString()
  }else if(b == 'NaN'){
    b = 0
  }
  var m = Math.pow(10, digit);
  var h = Math.pow(m, 2);
  var x = 0 ;var y = 0;var xflot='';var yflot='';
  if(a.indexOf('.')== -1){
    x = a
  }else{
    x = a.split('.')[0]
    xflot = a.split('.')[1]
  }
  if(b.indexOf('.')== -1){
    y = b
  }else{
    y = b.split('.')[0]
    yflot = b.split('.')[1]
  }
  var n1 = Math.pow(10, digit-xflot.length);
  var n2 = Math.pow(10, digit-yflot.length);
  var s = (Number(x)*Number(y)) + ((Number(x)*Number(yflot)*n2+ Number(y)*Number(xflot)*n1)*m+(Number(yflot)*n2)*(Number(xflot)*n1))/h
  return s.toFixed(5)
}


export const accurateAddEight =(formula,digit) =>{
  var m = Math.pow(10, digit);
  var list = formula.split('+')
  var IntList= []
  var flotList=[]
  var IntSub=0
  var flotSub=0
  list.map(item=>{
    if(item.indexOf('.') == -1){
      IntList.push(item)
    }else{
      var a = item.split('.')[0]
      var b = item.split('.')[1]
      IntList.push(a)
      flotList.push(b)
    }
  })
  IntList.map(item=>{
    IntSub+=Number(item)
  })
  flotList.map(item=>{
    var n = Math.pow(10, digit-item.length);
    flotSub+=Number(item)*n
  })
  var s = IntSub + flotSub/m
  return s.toFixed(8)
}

export const accurateReduceEight =(a,b,digit) =>{
if(typeof(a) == 'number' && a!='NaN'){
  a = a.toString()
}else if(a == 'NaN'){
  a = 0
}
if(typeof(b) == 'number' && b!='NaN'){
  b = b.toString()
}else if(b == 'NaN'){
  b = 0
}
var m = Math.pow(10, digit);
var x = 0 ;var y = 0;var xflot='';var yflot='';
if(a.indexOf('.')== -1){
  x = a
}else{
  x = a.split('.')[0]
  xflot = a.split('.')[1]
}
if(b.indexOf('.')== -1){
  y = b
}else{
  y = b.split('.')[0]
  yflot = b.split('.')[1]
}
var n1 = Math.pow(10, digit-xflot.length);
var n2 = Math.pow(10, digit-yflot.length);
var s = (Number(x)-Number(y)) + (Number(xflot)*n1- Number(yflot)*n2)/m
return s.toFixed(8)
}

export const accurateMultipEight =(a,b,digit) =>{
if(typeof(a) == 'number' && a!='NaN'){
  a = a.toString()
}else if(a == 'NaN'){
  a = 0
}
if(typeof(b) == 'number' && b!='NaN'){
  b = b.toString()
}else if(b == 'NaN'){
  b = 0
}
var m = Math.pow(10, digit);
var h = Math.pow(m, 2);
var x = 0 ;var y = 0;var xflot='';var yflot='';
if(a.indexOf('.')== -1){
  x = a
}else{
  x = a.split('.')[0]
  xflot = a.split('.')[1]
}
if(b.indexOf('.')== -1){
  y = b
}else{
  y = b.split('.')[0]
  yflot = b.split('.')[1]
}
var n1 = Math.pow(10, digit-xflot.length);
var n2 = Math.pow(10, digit-yflot.length);
var s = (Number(x)*Number(y)) + ((Number(x)*Number(yflot)*n2+ Number(y)*Number(xflot)*n1)*m+(Number(yflot)*n2)*(Number(xflot)*n1))/h
return s.toFixed(8)
}